import typing

from app.models.my_application_models import UserApply, UserApplyDetail
from app.schemas.point_management.my_application_schemas import UserApplySchemas
from app.utils.point_management.user_apply_utils import create_oa_order_number


class UserApplyService:
    @staticmethod
    async def list(params: UserApplySchemas) -> typing.Dict:
        """
        获取申请列表
        :param params:
        :return:
        """
        data = await UserApply.get_list(params)
        return data

    @staticmethod
    async def save_or_update(params: UserApplySchemas) -> typing.Dict:
        """
        更新申请信息
        :param params:
        :return:
        """
        # user_apply_detail_param = params.get('userApply').get('userApplyDetails')
        user_apply_detail_param = params.userApply['userApplyDetails']
        params = params.userApply
        oa_serial_number = create_oa_order_number()
        params['oa_serial_number'] = oa_serial_number
        user_apply_info = await UserApply.create_or_update(params)
        if user_apply_info:
            apply_id = await UserApply.get_user_apply_id(params)
            for i in range(len(user_apply_detail_param)):

                # 扣分入库负分数
                award_deduction_type = user_apply_detail_param[i]['award_deduction_type']
                award_deduction_value = user_apply_detail_param[i]['award_deduction_value']
                if award_deduction_type == 20:
                    user_apply_detail_param[i]['award_deduction_value'] = -1 * abs(int(award_deduction_value))
                user_apply_detail_param[i]['apply_id'] = apply_id[0]['id']
                user_apply_detail_param[i]['apply_id'] = apply_id[0]['id']
                await UserApplyDetail.create_or_update(user_apply_detail_param[i])
            return params

    @staticmethod
    async def undo(params: UserApplySchemas) -> typing.Dict:
        """
        撤銷
        :param params:
        :return:
        """
        data = await UserApply.get_user_apply_by_id(params)
        if data:
            params = UserApplySchemas(**data)
            params.status = 50
        return await UserApply.create_or_update(params.dict())

    @staticmethod
    async def approve(params: UserApplySchemas) -> typing.Dict:
        """
        审批
        :param params:
        :return:
        """
        data = await UserApply.get(params.id)
        if data:
            return await UserApply.create_or_update(params.dict())

    @staticmethod
    async def detail(params: UserApplySchemas) -> typing.Dict:
        """
        审批
        :param params:
        :return:
        """
        data = await UserApply.get(params.id, to_dict=True)
        if data:
            detail_data = await UserApplyDetail.get_user_apply_detail_by_id(data['id'])
            if detail_data:
                data['userApplyDetails'] = detail_data
        return data

    @staticmethod
    async def deleted(params: UserApplySchemas) -> int:
        """
        删除申请
        :param params:
        :return:
        """
        data = await UserApply.get_user_apply_by_id(params)
        if data:
            # raise ParameterError(CodeEnum.PROJECT_HAS_MODULE_ASSOCIATION)
            return await UserApply.delete(params.id)
